package edu.cuit.campus.web.controller;

import edu.cuit.campus.po.Company;
import edu.cuit.campus.service.CompanyService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/19 9:47
 */
@Controller
@RequestMapping("/company")
public class CompanyController {
    @Resource private CompanyService service;

    @RequestMapping("/find")
    public String findById(@RequestParam(name = "id") Integer id, Model model){
        var company = service.findById(id);
        //将company对象设置到request共享作用域中
        model.addAttribute("company", company); //request.setAttribute("company", company);
        // /WEB-INF/pages/company.jsp
        return "company"; //request.getRequestDispatcher("/WEB-INF/pages/company.jsp").forward(request,response);
    }

    /**
     * RequestAttribute: 从请求作用域中获取共享属性值
     *      name/value: 属性名,默认为参数名
     *      required: 是否必须，默认为true
     */
    @ResponseBody
    @RequestMapping(path = "/show", produces = "application/json;charset=utf-8")
    public Company show(@RequestAttribute("company") Company company){
        return company;
    }

    @RequestMapping("/json")
    public String findById2Json(@RequestParam(name = "id") Integer id, Model model){
        var company = service.findById(id);
        //将company对象设置到request共享作用域中
        model.addAttribute("company", company); //request.setAttribute("company", company);
        /*
            Spring MVC约定：返回逻辑视图
            forward: 请求分发
            redirect: 请求重定向
         */
        return "forward:show"; //request.getRequestDispatcher("/company/show").forward(request,response);
    }

}
